/*
 * @Author: Creator
 * @Date: 2020-10-07 19:28:11
 * @LastEditors: Creator
 * @LastEditTime: 2020-10-09 18:23:43
 * @Description: file content
 */
#include "tools.h"
#include <stdbool.h>
#include <stdio.h>

bool strToFloat(char data[],float *target)
{
    float temp = 0;
    float base = 1;
    int i = 0;
    bool flag = 0;
    while(data[i])
    {
        if((data[i] >= '0' && data[i] <= '9') || 
            data[i] == '.')
        {
            if(data[i] == '.')
            {
                flag = 1;
                i++;
                continue;
            }
            temp = temp * 10 + data[i] - '0';
            if(flag)
                base *= 0.1;
            i++;
        }
        else
            return 0;
    }
    *target = temp * base;
    return 1;
}

bool getFloat(float *target)
{
    char data[10];
    scanf("%s",data);
    return strToFloat(data,target);
}

bool fgetFloat(float *target,FILE *fp)
{
    char data[10];
    fscanf(fp,"%[10]s",data);
    return strToFloat(data,target);
}

char* readID(FILE *fp)
{
    char id[19];
    fscanf(fp,"%[19]s",id);
    return id;
}

float readFloat(FILE *fp)
{
    float temp;
    fscanf("%f",&temp);
    return temp;
}
